import createID from '../../untils/id';
import schoolList from '../../config/school/school';
import lessonList from '../../config/school/lesson';
let  randomString =(len) => {
    　　len = len ||6;
    　　var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    　　var maxPos = $chars.length;
     　　var pwd = '';
     　　for (let i = 0; i < len; i++) {
     　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    　　}
   　　return pwd;
    } 
//获取学生列表；
let showStudentList = async (ctx,next) =>{
    let data = await ctx.db.querys(`select * from student`);
    const student =await ctx.db.querys(`select * from class`);
    return ctx.body = {code:1,msg:'成功获取学生列表',data:{data,student,schoolList,lessonList}}
}
let studentsAdd = async (ctx,next)=>{
    let isOpr = await ctx.isJudge(ctx,'A');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }
   //添加学生
   let {obj} = ctx.request.body
   obj.createtime = new Date().getTime()+'';
   let str = new Date().getTime()+''
   let id = str.substring(str.length-2)+randomString(6)
   obj.studentid = id
   obj.state = 'ok' //obj有传过来的数据，还有时间，状态，跟id;
   let obj2 ={}
   obj2.seat = obj.SetSeats+'';
   obj2.name = obj.name;
   obj2.classid = obj.classid
   let crr = await ctx.db.querys(`select * from class where classid='${obj.classid}'`)
   
   let obj1 = crr[0],
   brr = [];
   brr = await ctx.db.querys(`select * from student`)
   brr= brr.filter(item=>item.classid===obj.classid)
   if(obj1.type === '大班'&& brr.length>40) return ctx.body= {code:0,msg:'人员已满，请换一个班级'}
   if(obj1.type === '中班'&& brr.length>30) return ctx.body= {code:0,msg:'人员已满，请换一个班级'}
   if(obj1.type === '小班'&& brr.length>20) return ctx.body= {code:0,msg:'人员已满，请换一个班级'}
   console.log(obj)
   delete(obj["SetSeats"]);
   
   let arr = await ctx.db.insert('student',obj)
   console.log('我走了')
   if(!arr.affectedRows) return ctx.body={code:0,msg:"添加失败，请稍后再试"}  
  
   let add = await ctx.db.insert('seat',obj2)
   if(!add.affectedRows) return ctx.body = {code:0,msg:'座位添加失败请过会再试'}
   ctx.body ={code:1,msg:'添加成功'}
}
let getSchoolList = async (ctx,next) => {
    //获取学院信息,课程信息
    const student =await ctx.db.querys(`select * from class`);
    ctx.body = {code:1,msg:'请求成功',data:{schoolList,lessonList,student} }
}
let getSeat = async (ctx,next) => {
    //获取座位信息
    let {classid} = ctx.request.body
    let seatData = await ctx.db.querys (`select * from seat`)
    let arr = seatData.filter(item =>item.classid===classid)
    ctx.body = {code:1,msg:'获取数据成功',data:arr}
}
let addSeat = async (ctx,next) => {
    //添加座位
    let {obj} = ctx.request.body // 这个obj中 包含 seat 此为座位号  name 此为学生名字  classid 此为班级号 
    let add = await ctx.db.insert('seat',obj)
    if(!add.affectedRows) return ctx.body = {code:0,msg:'添加失败请过会再试'}
    ctx.body = {code:1,msg:'添加成功'}
}
let studentsEdit = async (ctx,next) =>{
    let isOpr = await ctx.isJudge(ctx,'E');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }
    //学生编辑
    let {obj} = ctx.request.body;
    let arr =  await ctx.db.querys(`select * from student where studentid='${obj.studentid}'`)
    let obj1 = arr[0]
    //如果前台传来的值是空值 或者漏传时 做个替换处理
    obj.name = obj.name?obj.name:obj1.name
    obj.age = obj.age? obj.age:obj1.age
    obj.sex = obj.sex? obj.sex:obj1.sex
    obj.education = obj.education? obj.education:obj1.education
    obj.schoolid = obj.schoolid? obj.schoolid:obj1.schoolid
    obj.classid = obj.classid? obj.classid:obj1.classid
    obj.courseid = obj.courseid? obj.courseid :obj1.courseid
    //将补充完整的obj 作为修改基准 更新数据库中的信息
    let edit =await ctx.db.querys(`update student set name='${obj.name}',age='${obj.age}',sex='${obj.sex}',education='${obj.education}',schoolid='${obj.schoolid}',classid='${obj.classid}',courseid='${obj.courseid}' where studentid='${obj.studentid}'`)
    if (!edit.affectedRows) return ctx.body = {code:0,msg:'编辑失败，请稍后再试'}
    ctx.body = {code:1,msg:'编辑成功'}
}

let studentSerch = async (ctx,next) => {
    //学生搜索
    let {obj} = ctx.request.body
    let arr = await ctx.db.querys(`select * from student`)
    console.log(obj)
    if (obj.name) {
        arr = arr.filter(item =>item.name.includes(obj.name))
    }
    if (obj.education) {
        arr = arr.filter(item => item.education===obj.education)
    }
    if (obj.courseid){
        obj.courseid = obj.courseid+''
        arr = arr.filter(item =>item.courseid === obj.courseid)
    }
    ctx.body = {code:1,msg:'查询完毕',data:arr}
}

export default {
    showStudentList,studentsAdd,getSchoolList,getSeat,addSeat,studentsEdit,studentSerch
}
